package com.yq.cns.ui.main.fragment.common

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager2
import base.BaseLazyFragment
import com.fmt.mvi.learn.gobal.log
import com.scwang.smart.refresh.header.ClassicsHeader
import com.yq.cns.R
import com.yq.cns.adapter.NewsListAdapter
import com.yq.cns.databinding.FragmentNewsCommonBinding
import com.yq.cns.entity.ChannelEntity
import kotlinx.coroutines.launch

class CommonNewsFragment : BaseLazyFragment<FragmentNewsCommonBinding>() {
    val mViewModel by viewModels<CommonNewsFragmentViewModel>()
    var channelEntity: ChannelEntity? = null
    private val newsListAdapter: NewsListAdapter by lazy { NewsListAdapter() }
    fun initArg(channelEntity: ChannelEntity) {
        this.channelEntity = channelEntity
//        try {
//            "CommonNewsFragment 地址$mViewModel".log()
//        }catch (e:Exception){
//            "CommonNewsFragment initArg 方法执行".log()
//        }

    }


    override fun loadPageData() {
        channelEntity?.let {
            mViewModel.getNewsList(it.cname)
        }

        "CommonNewsFragment 地址$mViewModel".log()

    }

    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentNewsCommonBinding {
        return FragmentNewsCommonBinding.inflate(inflater, container, false)
    }


    override fun initView() {
        super.initView()
        binding.mSRL.setRefreshHeader(ClassicsHeader(context))
        binding.mSRL.setEnableLoadMore(false)
        binding.mRv.layoutManager =
            LinearLayoutManager2(context)
        newsListAdapter.loadMoreModule.isEnableLoadMore = true
        newsListAdapter.loadMoreModule.setOnLoadMoreListener {
            channelEntity?.let {
                mViewModel.getNewsListMore(it.cname)
            }

        }
        binding.mRv.adapter = newsListAdapter
    }


    override fun initListener() {
        super.initListener()
        binding.mSRL.setOnRefreshListener {
            loadPageData()
        }
        lifecycleScope.launch {
            mViewModel.state.collect {
                when (it) {
                    is CommonNewsFragmentState.Loading -> {

                    }

                    is CommonNewsFragmentState.Error -> {

                    }

                    is CommonNewsFragmentState.LoadDataSuccess -> {
                        binding.mSRL.finishRefresh()
                        newsListAdapter.setList(it.datas)
                    }

                    is CommonNewsFragmentState.LoadMoreDataSuccess -> {
                        newsListAdapter.addData(it.datas)
                        newsListAdapter.loadMoreModule.loadMoreComplete()
                    }

                    is CommonNewsFragmentState.LoadMoreDataFail -> {
                        newsListAdapter.loadMoreModule.loadMoreFail()
                    }

                    is CommonNewsFragmentState.LoadMoreDataNoData -> {
                        newsListAdapter.loadMoreModule.loadMoreEnd()

                    }
                }
            }
        }

    }

    override fun getLayout() = R.layout.fragment_news_common


}